package main

import (
	"fmt"
	"log"
	clients "github.com/influxdata/influxdb/client/v2"
)

// influxdb demo

func connInflux() clients.Client {
	cli, err := clients.NewHTTPClient(clients.HTTPConfig{
		//Addr:     "https://ts-bp1f3var6yvm4h37k.influxdata.tsdb.aliyuncs.com:8086",
        Addr: "https://ts-bp111abgw2l6l318d.influxdata.tsdb.aliyuncs.com:8086",
		Username: "devops",
		Password: "2020iotMONITOR",
	})
	if err != nil {
		log.Fatal(err)
	}
	return cli
}

// query
func queryDB(cli clients.Client, cmd string) (res []clients.Result, err error) {
	q := clients.Query{
		Command:  cmd,
		Database: "prometheus",
	}
	if response, err := cli.Query(q); err == nil {
		if response.Error() != nil {
			return res, response.Error()
		}
		res = response.Results
	} else {
		return res, err
	}
	return res, nil
}

// insert

func main() {

	conn := connInflux()
	fmt.Println(conn)

	qs := `select value FROM "rp_7d"."dapr_http_server_latency_bucket" WHERE "app_id" = 'appliance-api' AND "job" = 'vm-dapr' AND time > now() - 5m GROUP BY *`
	res, err := queryDB(conn, qs)
	if err != nil {
		fmt.Println(111, err)
		return
	}
	fmt.Println(22222, res[0].Series)  //select value

	qs = `show series FROM "rp_7d"."dapr_http_server_latency_bucket" WHERE "app_id" = 'appliance-api' AND "job" = 'vm-dapr' AND time > now() - 5m `
	res, err = queryDB(conn, qs)
	if err != nil {
		fmt.Println(111, err)
		return
	}
	fmt.Println(22222, len(res[0].Series[0].Values))

}
